package com.jh.loadbalance.client.controller;

import com.jh.loadbalance.client.interceptor.LoadBalanceClientHttpRequestInterceptor;
import com.jh.loadbalance.client.service.feigns.SayingService;
import com.jh.loadbalance.client.service.rest.SayingRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
public class ClientController {

    // 自定义RestTemplate
    @Autowired
    private RestTemplate restTemplate;
    @Bean
    @Autowired
    @DependsOn("loadBalanceClientHttpRequestInterceptor")
    //@Qualifier
    public RestTemplate restTemplate(LoadBalanceClientHttpRequestInterceptor loadBalanceClientHttpRequestInterceptor) {
        RestTemplate bean = new RestTemplate();
        bean.setInterceptors(Arrays.asList(loadBalanceClientHttpRequestInterceptor));
        return bean;
    }


    /*@Bean
    @DependsOn("loadBalanceClientHttpRequestInterceptor")
    public Object custom(@Qualifier Collection<RestTemplate> restTemplates,
                         LoadBalanceClientHttpRequestInterceptor loadBalanceClientHttpRequestInterceptor) {
        *//**
         * @Qualifier将同类型的restTemplate注入到了参数里面，包含了loadBalance的restTemplate，所以拦截器使用了我们自定义注入的
         *//*
        restTemplates.forEach(temp -> {
            temp.setInterceptors(Arrays.asList(loadBalanceClientHttpRequestInterceptor));
        });
        return new Object();
    }*/

    @RequestMapping("/{serviceName}/message/{msg}")
    public String invokeMessage(@PathVariable String serviceName, @PathVariable String msg) {
        String uri = "/" + serviceName + "/message/" + msg;
        return restTemplate.getForObject(uri, String.class);
    }

    // Ribbon RestTemplate
    @Autowired
    @LoadBalanced
    private RestTemplate lbRestTemplate;

    @LoadBalanced
    @Bean
    public RestTemplate loadBalanceRestTemplate(){
        return new RestTemplate();
    }

    @RequestMapping("/{serviceName}/lb/message/{msg}")
    public String invokeLBMessage(@PathVariable String serviceName, @PathVariable String msg) {
        String uri = "http://" + serviceName + "/message/" + msg;
        return lbRestTemplate.getForObject(uri, String.class);
    }

    // Feign
    @Autowired
    private SayingService sayingService;

    @RequestMapping("/feign/message/{message}")
    public String invokeFeignMessage(@PathVariable String message) {
        return sayingService.say(message);
    }

    // Custom Rest
    @Autowired
    private SayingRestService sayingRestService;

    @RequestMapping("/rest/message/{message}")
    public String invokeRestMessage(@PathVariable String message) {
        return sayingRestService.say(message);
    }


}
